/*
 * BasicStop.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.core.transit;

import org.fpdev.core.basenet.BLink;
import org.fpdev.core.basenet.BNode;
import org.fpdev.util.FPUtil;

import java.awt.*;
import org.fpdev.core.basenet.BaseNetwork;
import org.fpdev.apps.rtemaster.gui.map.MapCanvas;

public class BasicStop extends Stop {

  public BasicStop(TransitPath tpath, int nodeID, int inPLI, int outPLI) {
    super(tpath, nodeID, "", inPLI, outPLI);
  }

  public BasicStop(TransitPath tpath, BNode node, int inPLI, int outPLI) {
    super(tpath, node, "", inPLI, outPLI);
  }

  @Override
  public void draw(MapCanvas canvas, int width, boolean annotate) {
    //Graph graph = canvas.getGraph();
    BaseNetwork baseNet = canvas.getBaseNet();
    BLink inLink = tpath_.getLink(inPLI_);
    BLink outLink = tpath_.getLink(outPLI_);
    //System.out.println("BasicStop::draw(), node="+node_.getID()+" inLink="+inLink.getID()+" outLink="+outLink.getID());
    //Vertex inOppV = graph.opposite(node_.getVertex(), inLink.getEdge());
    BNode inOppNode = baseNet.opposite(node_, inLink);
    //Vertex outOppV = graph.opposite(node_.getVertex(), outLink.getEdge());
    //System.out.println("bs:d node="+node_.getID()+" outLink="+outLink.getID());
    BNode outOppNode = baseNet.opposite(node_, outLink);

    double nx = node_.getX(), ny = node_.getY();
    double ivx = inOppNode.getX() - nx;
    double ivy = inOppNode.getY() - ny;
    double ivm = FPUtil.magnitude(nx, ny, inOppNode.getX(), inOppNode.getY());
    double ivxn = ivx * (1.0 / ivm);
    double ivyn = ivy * (1.0 / ivm);

    double ovx = outOppNode.getX() - nx;
    double ovy = outOppNode.getY() - ny;
    double ovm = FPUtil.magnitude(nx, ny, outOppNode.getX(), outOppNode.getY());
    double ovxn = ovx * (1.0 / ovm);
    double ovyn = ovy * (1.0 / ovm);

    double mx = ((nx + ivxn) + (nx + ovxn)) / 2.0;
    double my = ((ny + ivyn) + (ny + ovyn)) / 2.0;

    double mvx = mx - nx, mvy = my - ny;
    double mvm = FPUtil.magnitude(nx, ny, mx, my);

    double mvxn, mvyn;
    if (mvm < .00001) {
      double angle = -Math.PI / 2;
      double ca = Math.cos(angle);
      double sa = Math.sin(angle);
      mvxn = ovxn * ca - ovyn * sa;
      mvyn = ovxn * sa + ovyn * ca;
    } else {
      // normalize vectors
      mvxn = mvx * (1.0 / mvm);
      mvyn = mvy * (1.0 / mvm);

      if (java.awt.geom.Line2D.relativeCCW(inOppNode.getX(), inOppNode.getY(), nx, ny, outOppNode.getX(), outOppNode.getY()) == -1) {
        mvxn = -mvxn;
        mvyn = -mvyn;
      }
    }

    /*double mvxn = mvx*(1.0/mvm), mvyn = mvy*(1.0/mvm);
    if(java.awt.geom.Line2D.relativeCCW(inOppNode.getX(),inOppNode.getY(),nx,ny,outOppNode.getX(),outOppNode.getY())==-1) {
    mvxn = -mvxn;
    mvyn = -mvyn;
    }*/
    //canvas.getGraphics().setColor(Color.blue);
    //int width = 4;
    double length = 5;
    double res = canvas.getCC().getResolution();
    if (res > 50) {
      width = width / 2;
      length = 2 * length / 5;
    } else if (res > 25) {
      width = 3 * width / 4;
      length = 3 * length / 5;
    }
    canvas.getGraphics().setStroke(new BasicStroke(width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
    double nx2 = nx + canvas.getCC().dxToWorld(length) * mvxn;
    double ny2 = ny + canvas.getCC().dyToWorld(length) * mvyn;
    canvas.drawLine(nx, ny, nx2, ny2);
    canvas.drawPoint(nx2, ny2, width);
    /*canvas.setColor(Color.white);
    canvas.drawText(inPLI_+", "+outPLI_, nx, ny, 1, 1);
    canvas.setColor(Color.black);
    canvas.drawText(inPLI_+", "+outPLI_, nx, ny, 0, 0);*/
  }

  public int getType() {
    return STOP_BASIC;
  }

  public Stop createCopy(TransitPath newPath) {
    if(node_ != null) return new BasicStop(newPath, node_, inPLI_, outPLI_);
    return new BasicStop(newPath, nodeID_, inPLI_, outPLI_);
  }
}
















